/**
 * sliderWipe v1.0.0
 * @Date:   2021-04-21
 * @Author: Nikki
 */

;(function ($) {
  var o
  var initObj = {
    init: function (params) {
        this.creatNode(params)
      	  o = $.extend({
				  onConfirmed: function(imgeId) {
				},
			}, params || {});
    },
    creatNode: function (obj) {
      var nodes = ''
      var lenght = obj.data.length;
      if (obj && obj.data.length > 0) {
        for (let index = 0, len = obj.data.length; index < len ; index++) {
          const element = obj.data[index];
          nodes += '<li  key=" '+element.id +' ">\
                          <img src=' +element.img+ '>\
                        </li>\
                          '
        }
        $(obj.selector).append(nodes)
      }

      var initPos = $(obj.selector).find('img').width()
      $(obj.selector).css({ 'left': initPos + 'px' })
      
      if (lenght == 1) {
        $(obj.selector).siblings('right-btn').addClass('none')
      }

     this.bindEvent(obj)
 
    },
    bindEvent: function (obj) {
      var rightBtn = $(obj.selector).siblings('.right-btn');
      var leftBtn = $(obj.selector).siblings('.left-btn');
      var imgIndex = 0;
      var currPos
      var currVal

      // click right btn
      $(rightBtn).unbind('click')
      $(rightBtn).click(()=>{
        imgIndex++
        var itemW = $(obj.selector).find('li').eq(imgIndex).outerWidth(true)
        if (itemW) {
            if(currPos||currPos==0){
              currVal=currPos-itemW
              currPos = currVal
            }else{
              currVal= $(obj.selector).css('left').match(/\d+/)[0]-itemW
              currPos = currVal
            }
        }   

        if(imgIndex>=$(obj.selector).find('li').length-1){  
            imgIndex=$(obj.selector).find('li').length-1
            $(rightBtn).addClass('none')
                currPos=currVal
          }
        $(leftBtn).removeClass('none');
        $(obj.selector).css({ 'left': currVal + 'px', transition: 'left .3s ease-in-out' });
      })

      // click left btn
     $(leftBtn).unbind('click')
     $(leftBtn).click(()=>{
        $(rightBtn).removeClass('none')
        imgIndex--
        let initialIndex=0
        let leftVal
        let itemW=$(obj.selector).find('li').eq(imgIndex).outerWidth(true)
            if(Math.abs(currPos)==0){
                leftVal=$(obj.selector).find('img').width()
                currPos=leftVal
            }else{
                let absVal =Math.abs(currPos)-itemW
                leftVal= -absVal
                currPos=leftVal
                }
                    
            if(imgIndex<=initialIndex){  
                imgIndex=initialIndex
                $(leftBtn).addClass('none')
                leftVal=$(obj.selector).find('img').width()
                currPos=leftVal
                }
    
        $(obj.selector).css({ 'left': leftVal+ 'px',transition:'left .3s ease-in-out'})
     })
      
      // close
      var closeBtn = $(obj.selector).parents('.cover-modal').find('.cover-close');
      var confirmBtn = $(obj.selector).parents('.cover-modal').find('.cover-confirm');
      closeBtn.click(function () { $(this).parents('.cover-modal-bg').fadeOut() });
      confirmBtn.click(function () {
          var coverId = $(obj.selector).find('li').eq(imgIndex).attr('key')
          o.onConfirmed(coverId);
          $(this).parents('.cover-modal-bg').fadeOut()
      });

    },
  
  }

  $.fn.slider = function (obj) {
    if (typeof obj === 'object') {
			return initObj.init(obj)
		}
  }

})(jQuery)